package com.atldy.atm;

import com.atldy.atm.domain.Cuntos;
import com.atldy.atm.domain.User;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

/**
 * @author 唐多山
 * @create 2020-09-11 9:28
 */
public class ATM {

    public static List<User> listU;
    public static List<Cuntos> listC;

//    初始化数据
    static {
//        用户
        listU = new ArrayList<>();
        listU.add(new User("520 1314 6898 0", "天涯浪子0", "123", "13488669900", 10000));
        listU.add(new User("520 1314 6898 1", "天涯浪子1", "123", "13488669901", 10000));
        listU.add(new User("520 1314 6898 2", "天涯浪子2", "123", "13488669902", 10000));
        listU.add(new User("520 1314 6898 3", "天涯浪子3", "123", "13488669903", 10000));
        listU.add(new User("520 1314 6898 4", "天涯浪子4", "123", "13488669904", 10000));
        listU.add(new User("520 1314 6898 5", "天涯浪子5", "123", "13488669905", 10000));
        listU.add(new User("520 1314 6898 6", "天涯浪子6", "123", "13488669906", 10000));
        listU.add(new User("520 1314 6898 7", "天涯浪子7", "123", "13488669907", 10000));
        listU.add(new User("520 1314 6898 8", "天涯浪子8", "123", "13488669908", 10000));
        listU.add(new User("520 1314 6898 9", "天涯浪子9", "123", "13488669909", 10000));


//        管理员
        listC = new ArrayList<>();
        listC.add(new Cuntos("10", "天涯浪子10", "123", "13488669910", 10000, listU));
        listC.add(new Cuntos("11", "天涯浪子11", "123", "13488669910", 10000, listU));
        listC.add(new Cuntos("12", "天涯浪子12", "123", "13488669910", 10000, listU));
        listC.add(new Cuntos("13", "天涯浪子13", "123", "13488669910", 10000, listU));
        listC.add(new Cuntos("14", "天涯浪子14", "123", "13488669910", 10000, listU));
        listC.add(new Cuntos("15", "天涯浪子15", "123", "13488669910", 10000, listU));

    }

    public static void main(String[] args) {
//        登录功能判断
        login();
    }

    private static void login() {

        Scanner scannerU = new Scanner(System.in);

        Scanner scannerP = new Scanner(System.in);

        Scanner scannerC = new Scanner(System.in);

        for (int i = 0; i < listU.size() + listC.size(); i++) {

            System.out.println("***********************************");
            System.out.println("*************ATM取款机*************");
            System.out.println("*********欢迎使用ATM取款机**********");
            System.out.println("***********************************");

            System.out.println("请输入卡号：");
            String username = scannerU.nextLine();

            System.out.println("请输入密码：");
            String password = scannerP.nextLine();

//            管理员登录
            for (Cuntos cuntos : listC) {
                //        管理员登录判断登录
                if (username.equals(cuntos.getUsername()) && password.equals(cuntos.getPwd())) {
                    char at = username.charAt(1);
                    System.out.println("欢迎   [管理员" + at + "]  登录");
                    boolean flang = true;
                    while (flang) {
                        System.out.println("*****************************************");
                        System.out.println("********请输入你想要的操作类型：*********");
                        System.out.println("********1.查看用户        2.修改用户*********");
                        System.out.println("********3.删除            4.查看工资*********");
                        System.out.println("********5.退卡                  *********");
                        System.out.println("*****************************************");
//                        输入要操作的数字
                        int select = scannerC.nextInt();
//                        功能1、余额查询
                        flang = FindUserAll(select, cuntos);
                        if (!flang) {
                            return;
                        }
                    }
                }
            }

//            用户登录判断
            for (User user : listU) {
                if (username.equals(user.getUsername()) && password.equals(user.getPwd())) {
                    System.out.println("欢迎 "+user.getName()+" 顾客登录!请注意您的安全");
                    boolean flang=true;
                    while (flang) {
                        System.out.println("*****************************************");
                        System.out.println("********请输入你想要的操作类型：*********");
                        System.out.println("********1.余额查询         2.取款*********");
                        System.out.println("********3.转账             4.存款*********");
                        System.out.println("********5.退卡                  *********");
                        System.out.println("*****************************************");
//                        输入要操作的数字
                        int select = scannerC.nextInt();
//                        //判断是否是false，终止循环
                        flang=userCz(select,user);
                        if (!flang) {
                            return;
                        }
                    };
                }
            }
        }
    }

    private static boolean userCz(int select, User user) {
        Scanner scanner=new Scanner(System.in);
       final int moneys=user.getMoney();
        switch (select){
            case 1: //余额查询
                if (moneys!=0){
                    System.out.println("您的账户余额为："+moneys);
                    return true;
                }
                break;
            case 2: //取款
                System.out.println("***************************************");
                System.out.println("********请输入您想取款的数目：***********");
                System.out.println("********1.100           2.200 *********");
                System.out.println("********3.300           4.500 *********");
                System.out.println("********5.800           6.1000*********");
                System.out.println("********7.2000          8.其他**********");
                System.out.println("***************************************");
                int anInt = scanner.nextInt();
                int money=0; //余额
                switch (anInt){
                    case 1:
                        money=moneys-100;
                        System.out.println("请及时拿走现金 100 元");
                        break;
                    case 2:
                        money=moneys-200;
                        System.out.println("请及时拿走现金 200 元");
                        break;
                    case 3:
                        money=moneys-300;
                        System.out.println("请及时拿走现金 300 元");
                        break;
                    case 4:
                        money=moneys-500;
                        System.out.println("请及时拿走现金 500 元");
                        break;
                    case 5:
                        money=moneys-800;
                        System.out.println("请及时拿走现金 800 元");
                        break;
                    case 6:
                        money=moneys-1000;
                        System.out.println("请及时拿走现金 1000 元");
                        break;
                    case 7:
                        money=moneys-2000;
                        System.out.println("请及时拿走现金 2000 元");
                        break;
                    case 8:
                        System.out.println("请您输入要取的金额：");
                       int intMoney= scanner.nextInt();
                        money=moneys-intMoney;
                        System.out.println("请及时拿走现金 "+intMoney+" 元");
                        break;
                }
                System.out.println();
                System.out.println("****************************");
                System.out.println();
                System.out.println("您的余额为："+money);;
                if (money!=0){
                    user.setMoney(money);
                    return true;
                }
                break;
            case 3: //转账
                System.out.println("请输入对方的账户号码：");
                //输入的转张的卡号
                String usernameZ=scanner.nextLine();
                //余额
                int ye=0;
                for (User users:listU){
                    if (usernameZ.equals(users.getUsername())){
                        if (user.getUsername().equals(usernameZ)){
                            System.out.println();
                            System.out.println("对不起，你不能转账给自己！");
                            System.out.println();
                            return true;
                        }
                        System.out.println("请输入您需要转账的金额：");
                        System.out.println();
                        //转账金额
                        int moneyZ=scanner.nextInt();
                        //本账号减去金额
                        ye=moneys-moneyZ;
                        user.setMoney(ye);
                        //转账账号加上
                       int zz= users.getMoney()+moneyZ;
                       users.setMoney(zz);
                        user.setMoney(ye);
                        System.out.println();
                        System.out.println("****************************");
                        System.out.println("您的余额为："+ye);
                        System.out.println();
                        return true;
                    }
                }
                break;
            case 4: //存款
                System.out.println("请输入您想存入的金额：");
                int moneyC=scanner.nextInt();
                int ck=moneys+moneyC;
                if (ck!=0){
                   user.setMoney(ck);
                    System.out.println();
                    System.out.println("****************************");
                    System.out.println("您的账户余额为："+ck);;
                    System.out.println();
                    return true;
                }
                break;
            case 5:  //退卡
                System.out.println("您是否继续操作yes/no[Y/N]");
//                输入判断值
                String cz = scanner.nextLine();
                if (cz.equals("yes") || cz.equals("Y")) {
                    login();
                    return true;
                }
                if (cz.equals("no") || cz.equals("N")) {
                    return false;
                }
                break;
        }

        return true;
    }


    private static boolean FindUserAll(int select, Cuntos cuntos) {
        Scanner scanner = new Scanner(System.in);
        switch (select) {
            case 1: //查询全部用户功能
                System.out.println("账号   |" + " 姓名   " + "|   密码 " + " |  电话  " + "   |余额 ");
                for (User user : cuntos.getUsers()) {
                    System.out.println(user.getUsername() + "  |" + user.getName() + "    " + "|    " + user.getPwd() + " |   " + user.getPhone() + "  |    " + user.getMoney());
                }
                if (cuntos != null) {
                    return true;
                }
                break;
            case 2: //修改用户功能.
                //  传入要修改的账号
                System.out.println("请输入要修改的用户账号：");
                String upUname = scanner.nextLine();
                for (User user : cuntos.getUsers()) {
                    if (user.getUsername().equals(upUname)) {
                        // 姓名
                        System.out.println("姓名：[如果不修改此属性直接按回车键]");
                        String upName = scanner.nextLine();
                        // 密码
                        System.out.println("密码：[如果不修改此属性直接按回车键]");
                        String upPwd = scanner.nextLine();
                        //电话
                        System.out.println("电话：[如果不修改此属性直接按回车键]");
                        String upPhone = scanner.nextLine();
                        // 余额
                        System.out.println("余额：[如果不修改此属性直接按回车键]");
                        int upMoney = scanner.nextInt();
//                       修改
                        if (upName != null && upName.length() != 0) {
                            user.setName(upName);
                        }
                        if (upPwd != null && upPwd.length() != 0) {
                            user.setPwd(upPwd);
                        }
                        if (upPhone != null && upPhone.length() != 0) {
                            user.setPhone(upPhone);
                        }
                        if (upMoney >= 0) {
                            user.setMoney(upMoney);
                        }
                        System.out.println(user.getUsername() + "  |" + user.getName() + "    " + "|    " + user.getPwd() + " |   " + user.getPhone() + "  |    " + user.getMoney());
                        return true;
                    }
                }
                break;
            case 3: //删除用户
                System.out.println("请输入要删除的用户账号：");
                String uname = scanner.nextLine();
                int size = cuntos.getUsers().size();
                Iterator<User> iterator = cuntos.getUsers().iterator();
                while (iterator.hasNext()) {
                    User user = iterator.next();
                    if (user.getUsername().equals(uname)) {
                        iterator.remove(); //删除操作
                        if (size > cuntos.getUsers().size()) {
                            System.out.println("删除成功！");
                            return true;
                        } else {
                            System.out.println("删除失败");
                            return true;
                        }
                    }
                }
                break;
            case 4: //查看工资
                if (cuntos.getMoney() != null) {
                    System.out.println("您的工资为：" + cuntos.getMoney());
                    System.out.println();
                    return true;
                }
                break;
            case 5: //退卡
                System.out.println("您是否继续操作yes/no[Y/N]");
//                输入判断值
                String cz = scanner.nextLine();
                if (cz.equals("yes") || cz.equals("Y")) {
                    login();
                    return true;
                }
                if (cz.equals("no") || cz.equals("N")) {
                    return false;
                }
                break;
        }
        return true;
    }
}
